home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CreatingGames / Utilities / C / Mesa / samples / texture.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-03  |  7.7 KB  |  470 lines

  1. /*
  2.  * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that (i) the above copyright notices and this permission notice appear in
  7.  * all copies of the software and related documentation, and (ii) the name of
  8.  * Silicon Graphics may not be used in any advertising or
  9.  * publicity relating to the software without the specific, prior written
  10.  * permission of Silicon Graphics.
  11.  *
  12.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13.  * ANY KIND,
  14.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16.  *
  17.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
  18.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22.  * OF THIS SOFTWARE.
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include <fcntl.h>
  30. #include <unistd.h>
  31. #include <math.h>
  32. #include <stdlib.h>
  33. #include "gltk.h"
  34.  
  35.  
  36. GLenum doubleBuffer, directRender;
  37.  
  38. char *texFileName = 0;
  39. TK_RGBImageRec *image;
  40.  
  41. float *minFilter, *magFilter, *sWrapMode, *tWrapMode;
  42. float decal[] = {GL_DECAL};
  43. float modulate[] = {GL_MODULATE};
  44. float repeat[] = {GL_REPEAT};
  45. float clamp[] = {GL_CLAMP};
  46. float nr[] = {GL_NEAREST};
  47. float ln[] = {GL_LINEAR};
  48. float nr_mipmap_nr[] = {GL_NEAREST_MIPMAP_NEAREST};
  49. float nr_mipmap_ln[] = {GL_NEAREST_MIPMAP_LINEAR};
  50. float ln_mipmap_nr[] = {GL_LINEAR_MIPMAP_NEAREST};
  51. float ln_mipmap_ln[] = {GL_LINEAR_MIPMAP_LINEAR};
  52. GLint sphereMap[] = {GL_SPHERE_MAP};
  53.  
  54. GLenum doSphere = GL_FALSE;
  55. float xRotation = 0.0, yRotation = 0.0, zTranslate = -3.125;
  56.  
  57. GLint cube;
  58. float c[6][4][3] = {
  59.     {
  60.     {
  61.         1.0, 1.0, -1.0
  62.     },
  63.     {
  64.         -1.0, 1.0, -1.0
  65.     },
  66.     {
  67.         -1.0, -1.0, -1.0
  68.     },
  69.     {
  70.         1.0, -1.0, -1.0
  71.     }
  72.     },
  73.     {
  74.     {
  75.         1.0, 1.0, 1.0
  76.     },
  77.     {
  78.         1.0, 1.0, -1.0
  79.     },
  80.     {
  81.         1.0, -1.0, -1.0
  82.     },
  83.     {
  84.         1.0, -1.0, 1.0
  85.     }
  86.     },
  87.     {
  88.     {
  89.         -1.0, 1.0, 1.0
  90.     },
  91.     {
  92.         1.0, 1.0, 1.0
  93.     },
  94.     {
  95.         1.0, -1.0, 1.0
  96.     },
  97.     {
  98.         -1.0, -1.0, 1.0
  99.     }
  100.     },
  101.     {
  102.     {
  103.         -1.0, 1.0, -1.0
  104.     },
  105.     {
  106.         -1.0, 1.0, 1.0
  107.     },
  108.     {
  109.         -1.0, -1.0, 1.0
  110.     },
  111.     {
  112.         -1.0, -1.0, -1.0
  113.     }
  114.     },
  115.     {
  116.     {
  117.         -1.0, 1.0, 1.0
  118.     },
  119.     {
  120.         -1.0, 1.0, -1.0
  121.     },
  122.     {
  123.         1.0, 1.0, -1.0
  124.     },
  125.     {
  126.         1.0, 1.0, 1.0
  127.     }
  128.     },
  129.     {
  130.     {
  131.         -1.0, -1.0, -1.0
  132.     },
  133.     {
  134.         -1.0, -1.0, 1.0
  135.     },
  136.     {
  137.         1.0, -1.0, 1.0
  138.     },
  139.     {
  140.         1.0, -1.0, -1.0
  141.     }
  142.     }
  143. };
  144. static float n[6][3] = {
  145.     {
  146.     0.0, 0.0, -1.0
  147.     },
  148.     {
  149.     1.0, 0.0, 0.0
  150.     },
  151.     {
  152.     0.0, 0.0, 1.0
  153.     },
  154.     {
  155.     -1.0, 0.0, 0.0
  156.     },
  157.     {
  158.     0.0, 1.0, 0.0
  159.     },
  160.     {
  161.     0.0, -1.0, 0.0
  162.     }
  163. };
  164. static float t[6][4][2] = {
  165.     {
  166.     {
  167.         1.1,  1.1
  168.     },
  169.     {
  170.         -0.1, 1.1
  171.     },
  172.     {
  173.         -0.1, -0.1
  174.     },
  175.     {
  176.         1.1,  -0.1
  177.     }
  178.     },
  179.     {
  180.     {
  181.         1.1,  1.1
  182.     },
  183.     {
  184.         -0.1, 1.1
  185.     },
  186.     {
  187.         -0.1, -0.1
  188.     },
  189.     {
  190.         1.1,  -0.1
  191.     }
  192.     },
  193.     {
  194.     {
  195.         -0.1,  1.1
  196.     },
  197.     {
  198.         1.1, 1.1
  199.     },
  200.     {
  201.         1.1, -0.1
  202.     },
  203.     {
  204.         -0.1,  -0.1
  205.     }
  206.     },
  207.     {
  208.     {
  209.         1.1,  1.1
  210.     },
  211.     {
  212.         -0.1, 1.1
  213.     },
  214.     {
  215.         -0.1, -0.1
  216.     },
  217.     {
  218.         1.1,  -0.1
  219.     }
  220.     },
  221.     {
  222.     {
  223.         1.1,  1.1
  224.     },
  225.     {
  226.         -0.1, 1.1
  227.     },
  228.     {
  229.         -0.1, -0.1
  230.     },
  231.     {
  232.         1.1,  -0.1
  233.     }
  234.     },
  235.     {
  236.     {
  237.         1.1,  1.1
  238.     },
  239.     {
  240.         -0.1, 1.1
  241.     },
  242.     {
  243.         -0.1, -0.1
  244.     },
  245.     {
  246.         1.1,  -0.1
  247.     }
  248.     },
  249. };
  250.  
  251.  
  252. static void BuildCube(void)
  253. {
  254.     GLint i;
  255.  
  256.     glNewList(cube, GL_COMPILE);
  257.     for (i = 0; i < 6; i++) {
  258.     glBegin(GL_POLYGON);
  259.         glNormal3fv(n[i]); glTexCoord2fv(t[i][0]); glVertex3fv(c[i][0]);
  260.         glNormal3fv(n[i]); glTexCoord2fv(t[i][1]); glVertex3fv(c[i][1]);
  261.         glNormal3fv(n[i]); glTexCoord2fv(t[i][2]); glVertex3fv(c[i][2]);
  262.         glNormal3fv(n[i]); glTexCoord2fv(t[i][3]); glVertex3fv(c[i][3]);
  263.     glEnd();
  264.     }
  265.     glEndList();
  266. }
  267.  
  268. static void BuildLists(void)
  269. {
  270.  
  271.     cube = glGenLists(1);
  272.     BuildCube();
  273. }
  274.  
  275. static void Init(void)
  276. {
  277.  
  278.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  279.     gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image->sizeX, image->sizeY,
  280.               GL_RGB, GL_UNSIGNED_BYTE, image->data);
  281.     glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, decal);
  282.     glEnable(GL_TEXTURE_2D);
  283.  
  284.     glFrontFace(GL_CCW);
  285.     glCullFace(GL_FRONT);
  286.     glEnable(GL_CULL_FACE);
  287.  
  288.     BuildLists();
  289.  
  290.     glClearColor(0.0, 0.0, 0.0, 0.0);
  291.  
  292.     magFilter = nr;
  293.     minFilter = nr;
  294.     sWrapMode = repeat;
  295.     tWrapMode = repeat;
  296. }
  297.  
  298. static void Reshape(int width, int height)
  299. {
  300.  
  301.     glViewport(0, 0, (GLint)width, (GLint)height);
  302.  
  303.     glMatrixMode(GL_PROJECTION);
  304.     glLoadIdentity();
  305.     gluPerspective(145.0, 1.0, 0.01, 1000);
  306.     glMatrixMode(GL_MODELVIEW);
  307. }
  308.  
  309. static GLenum Key(int key, GLenum mask)
  310. {
  311.  
  312.     switch (key) {
  313.       case TK_ESCAPE:
  314.     tkQuit();
  315.  
  316.       case TK_LEFT:
  317.     yRotation -= 0.5;
  318.     break;
  319.       case TK_RIGHT:
  320.     yRotation += 0.5;
  321.     break;
  322.       case TK_UP:
  323.     xRotation -= 0.5;
  324.     break;
  325.       case TK_DOWN:
  326.     xRotation += 0.5;
  327.     break;
  328.       case TK_T:
  329.     zTranslate += 0.25;
  330.     break;
  331.       case TK_t:
  332.     zTranslate -= 0.25;
  333.     break;
  334.  
  335.       case TK_s:
  336.     doSphere = !doSphere;
  337.     if (doSphere) {
  338.         glTexGeniv(GL_S, GL_TEXTURE_GEN_MODE, sphereMap);
  339.         glTexGeniv(GL_T, GL_TEXTURE_GEN_MODE, sphereMap);
  340.         glEnable(GL_TEXTURE_GEN_S);
  341.         glEnable(GL_TEXTURE_GEN_T);
  342.     } else {
  343.         glDisable(GL_TEXTURE_GEN_S);
  344.         glDisable(GL_TEXTURE_GEN_T);
  345.     }
  346.     break;
  347.  
  348.       case TK_0:
  349.     magFilter = nr;
  350.     break;
  351.       case TK_1:
  352.     magFilter = ln;
  353.     break;
  354.       case TK_2:
  355.     minFilter = nr;
  356.     break;
  357.       case TK_3:
  358.     minFilter = ln;
  359.     break;
  360.       case TK_4:
  361.     minFilter = nr_mipmap_nr;
  362.     break;
  363.       case TK_5:
  364.     minFilter = nr_mipmap_ln;
  365.     break;
  366.       case TK_6:
  367.     minFilter = ln_mipmap_nr;
  368.     break;
  369.       case TK_7:
  370.     minFilter = ln_mipmap_ln;
  371.     break;
  372.  
  373.       default:
  374.     return GL_FALSE;
  375.     }
  376.     return GL_TRUE;
  377. }
  378.  
  379. static void Draw(void)
  380. {
  381.  
  382.     glClear(GL_COLOR_BUFFER_BIT);
  383.  
  384.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, sWrapMode);
  385.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, tWrapMode);
  386.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
  387.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
  388.  
  389.     glPushMatrix();
  390.  
  391.     glTranslatef(0.0, 0.0, zTranslate);
  392.     glRotatef(xRotation, 1, 0, 0);
  393.     glRotatef(yRotation, 0, 1, 0);
  394.     glCallList(cube);
  395.  
  396.     glPopMatrix();
  397.  
  398.     glFlush();
  399.  
  400.     if (doubleBuffer) {
  401.     tkSwapBuffers();
  402.     }
  403. }
  404.  
  405. static GLenum Args(int argc, char **argv)
  406. {
  407.     GLint i;
  408.  
  409.     doubleBuffer = GL_FALSE;
  410.     directRender = GL_TRUE;
  411.  
  412.     for (i = 1; i < argc; i++) {
  413.     if (strcmp(argv[i], "-sb") == 0) {
  414.         doubleBuffer = GL_FALSE;
  415.     } else if (strcmp(argv[i], "-db") == 0) {
  416.         doubleBuffer = GL_TRUE;
  417.     } else if (strcmp(argv[i], "-dr") == 0) {
  418.         directRender = GL_TRUE;
  419.     } else if (strcmp(argv[i], "-ir") == 0) {
  420.         directRender = GL_FALSE;
  421.     } else if (strcmp(argv[i], "-f") == 0) {
  422.         if (i+1 >= argc || argv[i+1][0] == '-') {
  423.         printf("-f (No file name).\n");
  424.         return GL_FALSE;
  425.         } else {
  426.         texFileName = argv[++i];
  427.         }
  428.     } else {
  429.         printf("%s (Bad option).\n", argv[i]);
  430.         return GL_FALSE;
  431.     }
  432.     }
  433.     return GL_TRUE;
  434. }
  435.  
  436. void main(int argc, char **argv)
  437. {
  438.     GLenum type;
  439.  
  440.     if (Args(argc, argv) == GL_FALSE) {
  441.     tkQuit();
  442.     }
  443.  
  444.     if (texFileName == 0) {
  445.     printf("No image file.\n");
  446.     tkQuit();
  447.     }
  448.  
  449.     image = tkRGBImageLoad(texFileName);
  450.  
  451.     tkInitPosition(0, 0, 300, 300);
  452.  
  453.     type = TK_RGB;
  454.     type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  455.     type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  456.     tkInitDisplayMode(type);
  457.  
  458.     if (tkInitWindow("Texture Test") == GL_FALSE) {
  459.     tkQuit();
  460.     }
  461.  
  462.     Init();
  463.  
  464.     tkExposeFunc(Reshape);
  465.     tkReshapeFunc(Reshape);
  466.     tkKeyDownFunc(Key);
  467.     tkDisplayFunc(Draw);
  468.     tkExec();
  469. }
  470.